-
Notifications
You must be signed in to change notification settings - Fork 58
Add IDE stubs generator service, docs, and tests #771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add IDE stubs generator service, docs, and tests #771
Conversation
you rock! |
Executing action: pyscript.generate_stubs
data: {} resulted in
as
Other than that, adding from stubs.pyscript_builtins import *
from stubs.pyscript_generated import * to my scripts resulted in flawless highlighting! It would be great to have the highlighting built-in into pyscript! |
Yeah, without proper IDE support, it’s really hard to write automations more complex than turning on a light from a motion sensor :) |
|
Wow, this is impressive! Thanks for the contribution. Still reading through the code, and so far looks great. I'll definitely merge this - hopefully tomorrow afternoon. |
Thanks! I’m glad it looks good so far. I can make any fixes or changes if needed. |
OMG, this is brilliant, thanks so much! Can't wait for it. When will we see this surfacing in HACS? |
How about an option to generate a __builtins__.pyi file in the main script directory? This would allow skipping the module imports into each script individually. I cobbled up my own basic version of pyscript_autocomplete in pyscript as a service that does this and it works pretty well in VS Code. |
My experiments with .pyi haven’t produced consistent results across IDEs. |
This PR is a reworked and improved version of pyscript_autocomplete.
Better docstrings and more accurate type definitions for the Pyscript runtime and for Home Assistant domains, services, and entities with attributes.
How it works:
pyscript.generate_stubs
reads the HA entity registry, skips disabled entries, and records each entity along with its attributes. Attributes with names that aren’t valid Python identifiers are listed and excluded from the final code._domain_state
class (subclass ofStateVal
) is created with the discovered attributes. Then a domain class is built where each entity is declared as a field of that state class.async_get_all_descriptions
, all HA service definitions are extracted. The generator converts selectors into python types, handles required/default fields and descriptions, and generates functions both at the domain level (as@staticmethod
) and on the entity state class (if the service supports a target).ast.unparse
.pyscript_builtins.py
and writes the generated module to /pyscript/modules/stubs/.from stubs import
andfrom stubs.* import
statements are ignored at runtime. Theoretically this could be a breaking change, but in practice, no one keeps real code in stubs.Example for
time
domain:In my HA installation, the generator produced about 5,900 lines of code, and it works perfectly.

Tested with PyCharm, VS Code, and Studio Code Server.